Ext.namespace('ZZExt.widgets');

ZZExt.widgets.PicPanel=Ext.extend(Ext.Panel,{
	uploadUrl:'imgUpload.action',
	savePath:'upload',
	defaultPic:'images/nopic.gif',
	baseUrl:'',//important;
	picWidth:180,
	picHeight:150,
	picLabel:'图片',
	imgExt:".jpg|.jpeg|.gif|.png|.bmp",
	photoField:null,
	constructor:function(config){
		Ext.apply(this,config);
	   
		
	   	ZZExt.widgets.PicPanel.superclass.constructor.call(this,{
	   		layout:'fit',
			items:[{
				xtype:'box',
				width:this.picWidth,
				height:this.picHeight,
				autoEl: {      
	                tag: 'img',    //指定为img标签      
	                src: this.defaultPic  
	            }      
			}],
			bbar:[this.picLabel,'->',{
				text:'上传',
				iconCls: 'upload-icon',
				handler:this.uploadPic,
				scope:this
			}]
	   });
    },
    getUploadWin:function(){
    	if(this.uploadWin!=null)return this.uploadWin;
    	var form = new Ext.form.FormPanel({
    		fileUpload:true,
    		layout:'column',
    		padding:5,
    		defaults:{columnWidth:0.98},
    		items:[new Ext.ux.form.FileUploadField({
		   	   emptyText: '请选择一张照片',
	           buttonText: '',
	           buttonCfg: {
	               iconCls: 'upload-icon'
	           }
			}),{
    			xtype:'tbtext',
    			text:'请选择合适文件:'+this.imgExt
    		}]
    	});
    	this.uploadWin = new Ext.Window({
    		title:'选择图片文件',
    		width:300,
    		frame:true,
    		items:[form],
    		buttons:[{text:'确定',handler:this.doUpload,scope:this},{text:'取消',handler:this.doCancel,scope:this}]
    	});
    	return this.uploadWin;
    },
    uploadPic:function(){
    	
    	this.getUploadWin().show();
    },
    isValidExt:function(fileName){
    	if(fileName=='')return false;
    	var ext = fileName.substring(fileName.lastIndexOf('.'));
    	if(this.imgExt.indexOf(ext.toLowerCase())<0){
    		Ext.Msg.alert('提示','请选择合法的图片文件');
    		return false;
    	}
    	return true;
    },
    doUpload:function(){
    	var win = this.getUploadWin();
    	var form = win.items.itemAt(0).getForm();
    	
    	var pic = form.findField(0).getValue();
    	if(!this.isValidExt(pic)){
    		Ext.Msg.alert('提示','请选择合法的图片文件');
    		return ;
    	}
    	var picBox = this.items.itemAt(0);
    	var baseUrl = this.baseUrl;
    	var photoField = this.photoField;
    	form.submit({
 	    	waitMsg:'正在上传...',
 	    	url:this.uploadUrl,
 	    	params:{savePath:this.savePath},
 	    	success:function(form,action){
 	    	    Ext.Msg.alert('提示','上传成功！');
 	    	    try{
 	    	    	win.hide();
					picBox.getEl().dom.src=baseUrl+action.result.data;
					photoField.setValue(action.result.data);
 	    	    }catch(_e){
 	    	    	alert('出错:'+_e);
 	    	    }
 	        }
 	    });
    },
    doCancel:function(){
    	this.getUploadWin().hide();
    },
    setPic:function(url){
    	var picBox = this.items.itemAt(0);
    	if(!url)picBox.getEl().dom.src= this.defaultPic;
    	else picBox.getEl().dom.src=this.baseUrl+url;
    }
});

Ext.reg('photoFileUploadField',ZZExt.widgets.PicPanel);